/*
 * @Author: lvgrape lvgrape@outlook.com
 * @Date: 2024-04-25 09:14:20
 * @LastEditors: lvgrape lvgrape@outlook.com
 * @LastEditTime: 2024-05-15 18:40:35
 * @FilePath: \ZINO_RC_RT\ZINO\hardware\button\button.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "rtthread.h"
#include "pin.h"
#include "drv_common.h"
#include "zino.h"
#include "rc.h"
#define DBG_TAG "hw.button"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define KEY_CHANNEL_COUNT (6 + 8 + 8)
struct keyInfo
{
    rt_uint8_t act : 4;
    rt_uint8_t down : 1;
    rt_uint8_t up : 1;
    rt_uint8_t longPress : 1;
    rt_uint8_t io : 1;
    rt_uint8_t intervalTime;
    rt_uint8_t holdTime;
    rt_uint8_t duration;
};
struct keyInfo keyValues[KEY_CHANNEL_COUNT] = {0};

void button_trace_handle(void *p)
{

    const rt_uint8_t LONG_PRESS_TIME = 100;  // 长按多久生效
    const rt_uint8_t INTERVAL_TIME_SET = 20; // 两次按键检测超时
    const rt_uint8_t ANTI_SHAKE_TIME = 2;    // 按键防抖检测超时

    // const rt_uint8_t pins[KEY_CHANNEL_COUNT] = {s12_pin, s13_pin, s14_pin, s15_pin, s16_pin, s17_pin};

    gpio_pin_remap_config(SWJTAG_GMUX_100, TRUE); // NOTE PA13,PA14,PA15 for io
    LOG_D("JATAG-DP/SW-DP Disable");

    // for (rt_uint8_t i = 0; i < KEY_CHANNEL_COUNT; i++)
    // {
    //     rt_pin_mode(pins[i], PIN_MODE_INPUT_PULLUP);
    //     rt_pin_write(pins[i], PIN_HIGH);
    //     LOG_D("Init button[%d]", pins[i]);
    // }

    while (1)
    {

        keyValues[0].io = zinoCommonRcData.values.stick.RLR<300;
        keyValues[1].io = zinoCommonRcData.values.stick.RLR>1700;
        keyValues[2].io = zinoCommonRcData.values.stick.RUD<300;
        keyValues[3].io = zinoCommonRcData.values.stick.RUD>1700;
        keyValues[4].io = zinoCommonRcData.values.stick.LUD<300;
        keyValues[5].io = zinoCommonRcData.values.stick.LUD>1700;
        keyValues[6].io = zinoCommonRcData.values.stick.LLR<300;
        keyValues[7].io = zinoCommonRcData.values.stick.LLR>1700;

        keyValues[8].io = zinoCommonRcData.values.button.LL;
        keyValues[9].io = zinoCommonRcData.values.button.LR;
        keyValues[10].io = zinoCommonRcData.values.button.LD;
        keyValues[11].io = zinoCommonRcData.values.button.LU;

        keyValues[12].io = zinoCommonRcData.values.button.RL;
        keyValues[13].io = zinoCommonRcData.values.button.RR;
        keyValues[14].io = zinoCommonRcData.values.button.RD;
        keyValues[15].io = zinoCommonRcData.values.button.RU;
        
        keyValues[16].io = zinoCommonRcData.values.button.q;
        keyValues[17].io = zinoCommonRcData.values.button.d;
        keyValues[18].io = zinoCommonRcData.values.button.b;
        keyValues[19].io = zinoCommonRcData.values.button.p;
        keyValues[20].io = zinoCommonRcData.values.button.LT;
        keyValues[21].io = zinoCommonRcData.values.button.RT;

        for (rt_uint8_t i = 0; i < KEY_CHANNEL_COUNT; i++)
        {
            if (keyValues[i].io)
            {
                if (keyValues[i].holdTime < LONG_PRESS_TIME)
                {
                    keyValues[i].holdTime++;
                    if (keyValues[i].holdTime >= ANTI_SHAKE_TIME)
                    {
                        // if (keyValues[i].holdTime == ANTI_SHAKE_TIME)
                        // {
                        //     LOG_D("key[%d] down", i);
                        // }
                        keyValues[i].down = 1;
                        keyValues[i].intervalTime = INTERVAL_TIME_SET;
                    }
                }
                else
                {
                    keyValues[i].longPress = 1;
                    // LOG_D("key[%d] hold", i);
                }
            }
            else
            {
                keyValues[i].holdTime = 0;
                keyValues[i].longPress = 0;
                if (keyValues[i].down)
                {
                    if(!keyValues[i].longPress)
                        keyValues[i].act++;
                    keyValues[i].down = 0;
                }
                if (keyValues[i].intervalTime)
                {
                    keyValues[i].intervalTime--;
                    if (keyValues[i].intervalTime == 1)
                    {
                        LOG_D("key[%d] act:%d", i, keyValues[i].act);
                        keyValues[i].duration = 10;
                        // keyValues[i].act = 0;
                        // zinoCommonRcData.values.keyAct = keyValues[i].act;
                    }
                }
            }

            if (keyValues[i].duration >= 1)
            {
                keyValues[i].duration--;
                if(keyValues[i].duration == 1)
                {
                    keyValues[i].act = 0;                    
                }
            }
      
        }
        rc_pairing_trigger(zinoCommonRcData.values.button.d &&  zinoCommonRcData.values.button.b);        
        rt_thread_mdelay(20);
    }
}

int button_init(void)
{
    rt_thread_t thread = rt_thread_create("button", button_trace_handle, RT_NULL, 1024, 10, 20);
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
        LOG_D("button init");
    }
    else
    {
        LOG_E("button init fail");
    }
    return RT_EOK;
}

ZINO_APP_EXPORT(button_init);
